package com.example.crud_demo.business.material.service.impl;

import com.example.crud_demo.business.material.dto.MaterialDTO;
import com.example.crud_demo.business.material.dto.MaterialExcelDTO;
import com.example.crud_demo.business.material.dto.MaterialQueryDTO;
import com.example.crud_demo.business.material.entity.Material;
import com.example.crud_demo.business.material.mapper.MaterialMapper;
import com.example.crud_demo.business.material.service.MaterialService;
import com.example.crud_demo.sys.exception.CustomException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 陈粮
 * @contact  18434365993
 */
@Service
public class MaterialServiceImpl implements MaterialService {
    final private MaterialMapper materialMapper;

    public MaterialServiceImpl(MaterialMapper materialMapper) {
        this.materialMapper = materialMapper;
    }

    @Override
    public Material createMaterial(MaterialDTO materialDTO) {
        // 检查物料名称是否已经存在，存在则不重复创建
        Material existingMaterial = materialMapper.selectByName(materialDTO.getName());
        if (existingMaterial != null) {
            throw new CustomException("物料名称已存在");
        }
        Material material = new Material();
        BeanUtils.copyProperties(materialDTO, material);
        material.setCreateTime(new Date());
        material.setUpdateTime(new Date());
        materialMapper.insert(material);
        return materialMapper.selectById(material.getId());
    }

    @Override
    public Material updateMaterial(MaterialDTO materialDTO) {
        Material material = materialMapper.selectByIdAndVersion(materialDTO.getId(), materialDTO.getVersion());
        if(material == null) {
            throw new CustomException("物料不存在，或版本不对应");
        }
        BeanUtils.copyProperties(materialDTO, material);
        material.setUpdateTime(new Date());
        materialMapper.update(material);
        return materialMapper.selectById(material.getId());
    }

    @Override
    public void deleteMaterial(Long id, Integer version) {
        // 检查物料是否已经存在
        Material existingMaterial = materialMapper.selectByIdAndVersion(id, version);
        if (existingMaterial == null) {
            throw new CustomException("物料不存在，或版本不对应");
        }
        materialMapper.delete(id, version);
    }

    @Override
    public Material getMaterialById(Long id) {
        return materialMapper.selectById(id);
    }

    @Deprecated
    @Override
    public List<Material> getAllMaterials() {
        return materialMapper.selectAll();
    }

    @Override
    public PageInfo<Material> page(MaterialQueryDTO query) {
        // 分页处理
        PageHelper.startPage(query.getPage(), query.getSize());
        List<Material> materials = materialMapper.select(query);
        return new PageInfo<>(materials);
    }

    @Override
    public void importMaterials(List<MaterialExcelDTO> materialDTOs) {
        for (MaterialExcelDTO materialDTO : materialDTOs) {
            Material material = new Material();
            BeanUtils.copyProperties(materialDTO, material);
            /**
             * 允许Excel批量修改或者更新，根据id判断
             */
            if(material.getId() != null) {
                // 修改
                material.setUpdateTime(new Date());
                materialMapper.update(material);
            } else {
                // 新增
                material.setCreateTime(new Date());
                material.setUpdateTime(new Date());
                materialMapper.insert(material);
            }
        }
    }

    @Override
    public List<MaterialExcelDTO> exportMaterials(MaterialDTO query) {
        List<Material> materials = materialMapper.select(query);
        return materials.stream().map(material -> {
            MaterialExcelDTO materialDTO = new MaterialExcelDTO();
            BeanUtils.copyProperties(material, materialDTO);
            return materialDTO;
        }).collect(Collectors.toList());
    }
}
